//
//  RegistViewController.m
//  Tospur-text
//
//  Created by Tospur_hewei on 15/10/12.
//  Copyright © 2015年 Tospur_hewei. All rights reserved.
//

#import "RegistViewController.h"
#import <AFNetworking.h>
#import <MBProgressHUD.h>
#import "Encryption.h"
@interface RegistViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *UserNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *PassNumTextField;
@property (weak, nonatomic) IBOutlet UITextField *PassAgainTextField;
@property (weak, nonatomic) IBOutlet UITextField *PhoneNumTextField;

@property (nonatomic,strong) MBProgressHUD *HUD;
@property (nonatomic,strong) NSString *status;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)quxiaoButtonAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (IBAction)registButtonAction:(id)sender {
    if (self.PassNumTextField.text != self.PassAgainTextField.text) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"两次密码输入不一致" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
    else
    {
        _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        _HUD.labelText=@"正在注册...";
        
        AFHTTPRequestOperationManager *tx = [AFHTTPRequestOperationManager manager];
        //申明返回的结果是json类型
        tx.responseSerializer = [AFJSONResponseSerializer serializer];
        //申明请求的数据是json类型
        tx.requestSerializer = [AFJSONRequestSerializer serializer];
        // [tx.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
        NSString *urlStr = @"http://172.20.5.142:7001/api/PusherRegister";
        NSString *signature = [[Encryption shareInstance]sha1];
        NSString *passStr = [NSString stringWithFormat:@"%@%@",self.PassNumTextField.text,@"tospur"];
        NSDictionary *dict = @{
                               @"appkey": @"tospur123",
                               @"signature": signature,
                               //@"sessionid": @"45785",
                               @"UserName": self.UserNameTextField.text,
                               @"Password": [[Encryption shareInstance]md5:passStr],
                               @"Mobile": self.PhoneNumTextField.text,
                               };
        [tx POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"登录返回的数据 === %@",responseObject);
//            self.userID = [responseObject objectForKey:@"userID"];
//            self.userName = [responseObject objectForKey:@"userName"];
//            self.sessionid = [responseObject objectForKey:@"sessionid"];
            self.status = [responseObject objectForKey:@"status"];
//
            [_HUD hide:YES afterDelay:1];
            
            //[self alertWarning];
            
            if ([self.status isEqualToString:@"200"])
            {
                _HUD.labelText=@"注册成功";
                
                [self dismissViewControllerAnimated:YES completion:nil];
              
            }
            else
            {
                _HUD.labelText=@"注册失败";
            }
            //登录记录
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
            
            
        }];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
